import { Injectable } from '@nestjs/common';

import { createTransport, Transporter } from "nodemailer";

import { ConfigService } from "@nestjs/config";

@Injectable()
export class EmailService {

    transport: Transporter;

    constructor(private configService: ConfigService) {
        this.transport = createTransport({
            host: this.configService.get("nodemailer_host"),
            port: this.configService.get("nodemailer_port"),
            secure: true,
            auth: {
                user: this.configService.get("nodemailer_auth_user"),
                pass: this.configService.get("nodemailer_auth_code")
            }
        });
    }

    async sendMail({ to, subject, html }) {
        await this.transport.sendMail({
            from: {
                name: "会议室预定系统",
                address: this.configService.get("nodemailer_auth_user")
            },
            to,
            subject,
            html
        })
    }
}
